package com.ooo.demo;



import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.a.sdk.SDKApi;
import com.a.sdk.mvc.interfaces.SDKListener;
import com.a.sdk.open.bean.GameRoleData;
import com.a.sdk.open.bean.PayData;
import com.a.sdk.utils.Logs;

import org.json.JSONObject;

public class MainActivity extends Activity implements View.OnClickListener {

    private TextView tvMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );//sensorLandscape
        setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE );//sensorLandscape


        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
            getWindow().setAttributes(lp);
        }
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);
        tvMsg = findViewById(R.id.tv_msg);
        findViewById(R.id.init).setOnClickListener(this);
        findViewById(R.id.login).setOnClickListener(this);
        findViewById(R.id.pay).setOnClickListener(this);
        findViewById(R.id.synchroniGameData).setOnClickListener(this);
        findViewById(R.id.tv_logout).setOnClickListener(this);
        findViewById(R.id.tv_exit).setOnClickListener(this);

        SDKApi.getInstance().setSDKListener(new SDKListener() {
            @Override
            public void onInitSuccess() {
                tvMsg.setText("初始化成功");
            }

            @Override
            public void onInitFail(int code, String msg) {
                tvMsg.setText("初始化失败"+code+msg);
            }

            @Override
            public void onLoginSuccess( String uid ,String token , String extra ) {
                try {
                    Logs.i(uid,token);
                    if( TextUtils.isEmpty( extra ) ){//如果为空
                        //研发读取本地配置AppConfig

                    }else {//如果不为空

                        JSONObject jsonObject = new JSONObject(extra);
                        String plat_id = jsonObject.getString("plat_id");
                        String channel_id = jsonObject.getString("channel_id");
                        Logs.i(plat_id,channel_id);

                    }


                    tvMsg.setText("登录成功"+uid+" "+token);
                }catch (Exception e){

                }
            }

            @Override
            public void onLoginFail(int code, String msg) {
                tvMsg.setText("登录失败"+code+msg);
            }

            @Override
            public void onLogOutSuccess() {
                tvMsg.setText("退出登录成功");
            }

            @Override
            public void onLogOutFail(int code, String msg) {
                tvMsg.setText("退出登录失败"+code+msg);
            }

            @Override
            public void onSubmitDataSuccess() {
                tvMsg.setText("数据同步成功");
            }

            @Override
            public void onSubmitDataFail(int code, String msg) {
                tvMsg.setText("数据同步失败"+code+msg);
            }

            @Override
            public void onPayComplete(String orderID) {
                tvMsg.setText("支付成功"+orderID);
            }

            @Override
            public void onPayFail(String orderID) {
                tvMsg.setText("支付失败"+orderID);
            }

            @Override
            public void onExitGame() {
                tvMsg.setText("SDK主动退出游戏取消");
            }

            @Override
            public void onExitGameCancel() {
                tvMsg.setText("SDK退出游戏取消");
            }

        });


//        Logs.i("123******",Settings.System.getString(this.getContentResolver(),Settings.Secure.ANDROID_ID));

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if( id == R.id.init ){
            SDKApi.getInstance().init( this );
        }else if( id == R.id.login ){
            SDKApi.getInstance().login(  );
        }else if( id == R.id.pay ){
            PayData payData = new PayData();
//            payData.setUid(uid);//用戶id
            payData.setCporder("020718544221118");//CP訂單號(測試時隨機六位數)
            // FIXME 此服務器ID 必須是當前玩家所在的遊戲區服ID，不能隨便亂填，否則會導致統計上的一系列問題！！ （注意：區服(必須是正整數)，沒有區服，傳1）
            payData.setSid("123456");
            payData.setAmount("800");//訂單金額(單位：分)
//                payData.setItem_id("10000");//商品ID(可傳可不傳，不傳為空)
            payData.setItem_id("com.indie.jzbhl.ft.pay8");//商品ID(可傳可不傳，不傳為空)
            payData.setItem_name("黃金月卡");//商品名稱
            payData.setExtra("8569123654");//CP透傳字段，通知CP支付結果時原樣返回
            payData.setRole_id("123321");//角色的唯一ID，4.4.2版本新增的參數
            payData.setRole_name("天下無敵");//角色名，5.2.0版本新增的參數
            SDKApi.getInstance().pay( payData );
        }else if( id == R.id.synchroniGameData ){

            GameRoleData gameRoleData = new GameRoleData();
//            gameRoleData.setUid(uid);//用戶id
            gameRoleData.setServer_id("10000");// FIXME 此服務器ID 必須是當前玩家所在的遊戲區服ID，不能隨便亂填，否則會導致統計上的一系列問題！！ （注意：區服(必須是正整數)，沒有區服，傳1）
            gameRoleData.setServer_name("電信");//服務器名稱
            gameRoleData.setRole_id("685321");//角色ID，一個角色同一個服ID保持唯一,長度不超過50
            gameRoleData.setRole_name("一血");//角色昵稱,長度不超過50
            gameRoleData.setRole_level(10);//遊戲等級
            long time1=Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10));//十位時間戳
            gameRoleData.setRole_create_time(time1);//角色創建時間(單位：秒，長度：10)
            SDKApi.getInstance().synchroniGameData( gameRoleData );

        }else if( id == R.id.tv_logout ){
            SDKApi.getInstance().logout(  );
        }else if( id == R.id.tv_exit ){
            SDKApi.getInstance().gameExit(  );
        }

    }




    /* Activity生命周期 必接*/
    @Override
    protected void onStart() {
        super.onStart();
        SDKApi.getInstance().onStart(MainActivity.this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        SDKApi.getInstance().onResume(MainActivity.this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SDKApi.getInstance().onRestart(MainActivity.this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        SDKApi.getInstance().onPause(MainActivity.this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKApi.getInstance().onStop(MainActivity.this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SDKApi.getInstance().onDestroy(MainActivity.this);

    }

    public void onActivityResult( int requestCode, int resultCode, Intent data ) {
        SDKApi.getInstance().onActivityResult( requestCode , resultCode , data );
    }




}